/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.jSkyRoom.factory;

import ru.jSkyRoom.generator.command.ModificationCommand;
import ru.jSkyRoom.interfaces.major.Enchantment;
import ru.jSkyRoom.interfaces.major.Major;
import ru.jSkyRoom.interfaces.major.NamedMajor;
import ru.jSkyRoom.interfaces.additional.HasEnchantment;
import ru.jSkyRoom.interfaces.additional.HasTemplate;

/**
 *
 * @author Rakev
 */
public class SimpleMajorModificationCommandFactory {

    private static SimpleMajorModificationCommandFactory instance = new SimpleMajorModificationCommandFactory();

    public static SimpleMajorModificationCommandFactory create() {
        return instance;
    }

    protected SimpleMajorModificationCommandFactory() {
    }

    public <T extends Major> ModificationCommand<T> createEnchantmeningtModificationCommand(final Enchantment enchantment) {
        return new ModificationCommand<T>() {
            @Override
            public T apply(T modificatingObject) {
                if (modificatingObject instanceof HasEnchantment) {
                    ((HasEnchantment) modificatingObject).setEnchantment(enchantment);
                }
                return modificatingObject;
            }
        };
    }

    public <T extends Major> ModificationCommand<T> createTemplatingModificationCommand(final T template) {
        return new ModificationCommand<T>() {
            @Override
            public T apply(T modificatingObject) {
                if (modificatingObject instanceof HasTemplate) {
                    ((HasTemplate) modificatingObject).setTemplate(template);
                }
                return modificatingObject;
            }
        };
    }

    public <T extends Major> ModificationCommand<T> createNamingModificationCommand(final String newName) {
        return new ModificationCommand<T>() {
            @Override
            public T apply(T modificatingObject) {
                if (modificatingObject instanceof NamedMajor) {
                    NamedMajor namingModificatingObject = (NamedMajor) modificatingObject;
                    namingModificatingObject.setName(newName);
                }
                return modificatingObject;
            }
        };
    }

    public <T extends Major> ModificationCommand<T> createConcatNamingModificationCommand(final String nameAppendix) {
        return new ModificationCommand<T>() {
            @Override
            public T apply(T modificatingObject) {
                if (modificatingObject instanceof NamedMajor) {
                    NamedMajor namingModificatingObject = (NamedMajor) modificatingObject;
                    String newName = namingModificatingObject.getName() + nameAppendix;
                    namingModificatingObject.setName(newName);
                }
                return modificatingObject;
            }
        };
    }
}
